package com.jy.adscm.operation.aftersales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.*;
import com.jy.adscm.operation.aftersales.mapper.mysql.*;
import com.jy.adscm.operation.aftersales.service.ShRepairService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.mscyb.bean.JqbzSettings;
import com.jy.adscm.operation.mscyb.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-21
 */
@Service
public class ShRepairServiceImpl extends ServiceImpl<ShRepairMapper, ShRepair> implements ShRepairService {
    @Autowired
    JyUserMapper  jyUserMapper;
    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
    @Autowired
    ShMachineMapper shMachineMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Autowired
    ShRepairSettingProecssMapper shRepairSettingProecssMapper;
    @Autowired
    ShRepairProecssMapper shRepairProecssMapper;
    @Autowired
    ShSpacePartsMapper shSpacePartsMapper;
    @Override
    public boolean insertShRepair(ShRepair shRepair) {

        shRepair.setIsBegin(0);
        shRepair.setIsEnd(0);
        shRepair.setIsStart(0);
        shRepair.setState(0);
        boolean insert = insert(shRepair);
        return insert;
    }

    @Override
    public boolean updateShRepair(ShRepair shRepair, Token token) throws AdscmSystemException {
          //查询上一次的结果
        ShRepair oldShRepair = super.selectById(shRepair.getId());
         //判断是否启动了
        if(shRepair.getIsStart()==1){
            if (oldShRepair.getIsStart()==0){
                shRepair.setStartTime(new Date());
                if (!AdscmBaseUtils.getInstance().isNotEmpty(shRepair.getDealType(),null)){
                   throw  new  AdscmSystemException("处理方式未设置");
                }
                //维修
                if ( shRepair.getDealType()==0){
                    //生成维修过程记录
                    Wrapper<ShRepairSettingProecss> shRepairSettingProecssWrapper=new EntityWrapper<>();
                    shRepairSettingProecssWrapper.eq("pid",shRepair.getPartName());
                    List<ShRepairSettingProecss> shRepairSettingProecsses = shRepairSettingProecssMapper.selectList(shRepairSettingProecssWrapper);
                    for (ShRepairSettingProecss shRepairSettingProecss : shRepairSettingProecsses) {
                         ShRepairProecss shRepairProecss=new ShRepairProecss();
                        shRepairProecss.setName(shRepairSettingProecss.getName());
                        shRepairProecss.setReference(shRepairSettingProecss.getReference());
                        shRepairProecss.setUnit(shRepairSettingProecss.getUnit());
                        shRepairProecss.setType(shRepairSettingProecss.getType());
                        shRepairProecss.setRemark(shRepairSettingProecss.getRemark());
                        shRepairProecss.setPid(shRepair.getId());
                        shRepairProecss.setRank(shRepairSettingProecss.getRank());
                        shRepairProecssMapper.insert(shRepairProecss);
                    }
                    shRepair.setState(1);
                }
                //报废
                if ( shRepair.getDealType()==1){
                    shRepair.setState(2);
                }

            }

            //设置开始时间
            if (shRepair.getIsBegin()==1){
                if (oldShRepair.getIsBegin()==0){
                    shRepair.setBeginAddTime(new Date());
                }
            }
            //设置结束时间
            if (shRepair.getIsEnd()==1){
                if (oldShRepair.getIsEnd()==0){
                    shRepair.setEndAddTime(new Date());
                }
            }
            //结果设置
            if(AdscmBaseUtils.getInstance().isNotEmpty(shRepair.getResult(),null)){
                if (!AdscmBaseUtils.getInstance().isNotEmpty(oldShRepair.getResult(),null)){
                    if (shRepair.getResult()==1){
                        //入备件库


                    }
                    shRepair.setState(2);
                }
            }
        }
        boolean b = super.updateById(shRepair);
        return b;
    }

    @Override
    public Page<ShRepair> selectShRepair(Page<ShRepair> page, QueryShRepair queryShRepair) {
        Wrapper<ShRepair> wrapper=new EntityWrapper<>();
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShRepair.getName(),null)){
            wrapper.eq("parts_name",queryShRepair.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryShRepair.getCode(),null)){
            wrapper.like("parts_code",queryShRepair.getCode());
        }
        wrapper.eq("state",queryShRepair.getState());
       page= super.selectPage(page,wrapper);
        List<ShRepair> shRepairs = page.getRecords();
        for (ShRepair shRepair : shRepairs) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepair.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepair.getCreator());
                shRepair.setCreatorName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepair.getPerformer(),null)){
                JyUser jyUser = jyUserMapper.selectById(shRepair.getPerformer());
                shRepair.setPerformerName(jyUser.getName());
            }
            if(AdscmBaseUtils.getInstance().isNotEmpty(shRepair.getPartName(),null)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(shRepair.getPartName());
                shRepair.setPartNameName(jqbzSettings);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(shRepair.getMachine(),null)){
                ShMachine shMachine = shMachineMapper.selectById(shRepair.getMachine());
                shRepair.setMachineName(shMachine);
            }
            if(AdscmBaseUtils.getInstance().isNotEmpty(shRepair.getCustomer(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(shRepair.getCustomer());
                shRepair.setCustomerName(shCustomerInfo);
            }

        }
        page.setTotal(super.selectCount(wrapper));
        return page;
    }

    @Override
    public List<JyUser> selectRepairPersons() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("220");
        return jyUsers;
    }
}
